package jjn.round1;

/**
 * @author Jjn
 * @since 2022/8/18 00:06
 */
public class LeetCode977_SquaresOfASortedArray {

    public int[] sortedSquares(int[] nums) {
        int left = 0, right = nums.length - 1;
        int[] result = new int[nums.length];
        int end = nums.length - 1;
        while (left <= right) {
            int leftSquare = nums[left] * nums[left];
            int rightSquare = nums[right] * nums[right];
            if (leftSquare <= rightSquare) {
                result[end] = rightSquare;
                right--;
            } else {
                result[end] = leftSquare;
                left++;
            }
            end--;
        }
        return result;
    }
}
